package com.example.datapedestal.dataAllocation.controller;


import com.example.datapedestal.dataAllocation.controller.form.DbConfigListForm;
import com.example.datapedestal.dataAllocation.controller.form.PACsConfigListForm;
import com.example.datapedestal.dataAllocation.service.IDbConfigService;
import com.example.datapedestal.utils.FeflectUtil;
import com.example.datapedestal.utils.vo.CommonResp;
import com.example.datapedestal.utils.vo.ResultCode;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hcy
 * @since 2023-10-31
 */
@Api(tags="数据配置")
@RestController
@RequestMapping("/dataAllocation/db-config")
public class DbConfigController {
    @Autowired
    private IDbConfigService iDbConfigService;

    @ApiOperation("查询配置")
    @RequestMapping(method = RequestMethod.GET,value = "/getConfig")
    public CommonResp getConfig(@RequestParam @NotNull int type){
        return CommonResp.success(iDbConfigService.getConfig(type));
    }


    @ApiOperation("添加PACs配置")
    @RequestMapping(method = RequestMethod.POST,value = "/addPACsConfig")
    public CommonResp addPACsConfig(@RequestBody PACsConfigListForm paCsConfigListForm) throws Exception {
        if(FeflectUtil.isNull(paCsConfigListForm)){
            if(iDbConfigService.addDbConfig(paCsConfigListForm)==1){
                return CommonResp.success(null);
            }else{
                return CommonResp.failed(ResultCode.ERROR_PRIMARY_REPETITION);
            }
        }else{
            return CommonResp.failed(ResultCode.ERROR_ACCESS_FORBIDDEN);
        }
    }

    @ApiOperation("添加数据配置")
    @RequestMapping(method = RequestMethod.POST,value = "/addDbConfig")
    public CommonResp addDataBaseConfig(@RequestBody DbConfigListForm dbConfigListForm) throws Exception {
        if(FeflectUtil.isNull(dbConfigListForm)){
            return CommonResp.success(iDbConfigService.addDbConfig(dbConfigListForm));
        }else{
            return CommonResp.failed(ResultCode.ERROR_ACCESS_FORBIDDEN);
        }
    }

    @ApiOperation("修改数据配置")
    @RequestMapping(method = RequestMethod.POST,value = "/updateDataBaseDbConfig")
    public CommonResp updateDataBaseConfig(@RequestParam String param,@RequestBody DbConfigListForm dbConfigListForm) throws Exception {
        if(FeflectUtil.isNull(dbConfigListForm)){
            if(iDbConfigService.updateDbConfig(param,dbConfigListForm)==1){
                return CommonResp.success(null);
            }else{
                return CommonResp.failed(ResultCode.ERROP);
            }
        }else{
            return CommonResp.failed(ResultCode.ERROR_ACCESS_FORBIDDEN);
        }
    }

    @ApiOperation("修改PACs配置")
    @RequestMapping(method = RequestMethod.POST,value = "/updatePACsDbConfig")
    public CommonResp updatePACsConfig(@RequestParam String param,@RequestBody PACsConfigListForm paCsConfigListForm) throws Exception {
        if(FeflectUtil.isNull(paCsConfigListForm)){
            if(iDbConfigService.updateDbConfig(param,paCsConfigListForm)==1){
                return CommonResp.success(null);
            }else{
                return CommonResp.failed(ResultCode.ERROP);
            }
        }else{
            return CommonResp.failed(ResultCode.ERROR_ACCESS_FORBIDDEN);
        }
    }

    @ApiOperation("删除数据配置")
    @RequestMapping(method = RequestMethod.POST,value = "/deleteDbConfig")
    public CommonResp deleteConfig(@RequestParam String configName){
        if (iDbConfigService.deleteConfig(configName)==1){
            return CommonResp.success(null);
        }else{
             return CommonResp.failed(ResultCode.ERROP);
        }
    }

}

